<template>
    <div class="utilize">
      <div class="head-search">
        <search @onSubmit="onSearch">
          <el-form-item>
            <el-input placeholder="请输入内容"
                      v-model="searchData.val"
                      size="small"
                      clearable
                      class="input-with-select">
              <el-select v-model="searchData.select" clearable slot="prepend" placeholder="请选择">
                <el-option v-for="(item, index) in searchDic"
                           :key="index"
                           :label="item.label"
                           :value="item.value"></el-option>
              </el-select>
            </el-input>
          </el-form-item>
        </search>
      </div>

      <avue-crud :option="tableOption"
                 :table-loading="loading"
                 :data="tableData"
                 :page.sync="page"
                 ref="crud"
                 @row-del="rowDel"
                 @on-load="onLoad">
        <template slot-scope="{row}" slot="menu">
          <el-button type="text" icon="el-icon-view" @click="handleRed(row)">查看档案</el-button>
        </template>
      </avue-crud>

    </div>
</template>

<script>
import Search from '../../../components/search/search'
import { detUtilizeList, getSelfUtilizeList, getUtilizeList } from '../../../api/utilize/utilizeList'

export default {
  name: 'UtilizeArchivesList',
  props: {
    type: {
      type: String,
      default: ''
    },
    idRegister: {
      type: String,
      default: ''
    }
  },
  components: { Search },
  data () {
    return {
      loading: false,
      page: {
        pageSize: 20,
        currentPage: 1,
        pageSizes: [20, 40, 60, 80],
        total: 0
      },
      tableData: [],
      searchData: {
        select: 'tm',
        val: ''
      },
      searchDic: [{
        label: '利用人姓名',
        value: 'username'
      }, {
        label: '档号',
        value: 'dh'
      }, {
        label: '题名',
        value: 'tm'
      }, {
        label: '利用日期',
        value: 'gmtCreate'
      }]
    }
  },
  beforeRouteLeave (to, from, next) {
    from.meta.keepAlive = to.path === '/utilize/utilizeQuery/utilizeDetails'
    next()
  },
  computed: {
    tableOption () {
      return {
        align: 'center',
        menuAlign: 'center',
        header: false,
        border: true,
        size: 'medium',
        height: this.type === 'self' ? 'calc(100vh - 200px)' : 'calc(100vh - 180px)',
        index: true, // 显示序号
        indexLabel: '序号', // 显示序号
        selection: false, // 多选
        editBtn: false, // 行内编辑按钮
        delBtn: true, // 行内删除按钮
        viewBtn: false, // 行内查看按钮
        menuWidth: 200,
        column: [
          {
            label: '档案类别',
            prop: 'categoryName',
            width: 120,
            overHidden: true
          },
          {
            label: '档号',
            prop: 'dh',
            width: 150,
            overHidden: true
          },
          {
            label: '题名',
            prop: 'tm',
            minWidth: 300,
            align: 'left',
            overHidden: true
          },
          {
            label: '利用人姓名',
            prop: 'username',
            width: 120,
            overHidden: true
          },
          {
            label: '利用日期',
            prop: 'gmtCreate',
            width: 150,
            overHidden: true
          },
          {
            label: '操作用户',
            prop: 'userCreate',
            width: 120,
            overHidden: true
          }
        ]
      }
    }
  },
  watch: {
    idRegister (v) {
      if (v) {
        this.onLoad()
      }
    }
  },
  methods: {
    onLoad () {
      // 加载列表
      this.loading = true
      const query = {
        pageSize: this.page.pageSize,
        pageNo: this.page.currentPage
      }
      if (this.searchData.val) {
        query.params = [{
          op: 'like',
          name: this.searchData.select,
          value: this.searchData.val
        }]
      }
      const request = this.type === 'self' ? getSelfUtilizeList(this.idRegister, query) : getUtilizeList(query)
      request.then(res => {
        this.tableData = res.data.data.list
        this.page.total = res.data.data.total
        this.loading = false
      })
    },
    onSearch () {
      this.onLoad()
    },
    handleRed (row) {
      // 查看档案
      const query = {
        type: 'archives',
        idTable: row.idTable,
        id: row.idRecord
      }
      this.$router.push({
        path: '/utilize/utilizeQuery/utilizeDetails',
        query: query
      })
    },
    rowDel (row) {
      this.$confirm('此操作将永久删除该条信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        detUtilizeList([row.id]).then(() => {
          this.$message.success('删除成功！')
          this.onLoad()
        })
      }).catch(() => {
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.utilize{

}
</style>
